home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Archives / GNU / GNUPLOTsrc.lha / bf_test.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-22  |  3.5 KB  |  161 lines

  1. #ifndef lint
  2. static char *RCSid = "$Id: bf_test.c,v 1.6 1994/09/13 16:11:14 alex Exp $";
  3. #endif
  4.  
  5.  
  6. /*
  7.  * Test routines for binary files
  8.  * cc bf_test.c -o bf_test binary_files.o -lm
  9.  *
  10.  * Copyright (c) 1992 Robert K. Cunningham, MIT Lincoln Laboratory
  11.  *
  12.  */
  13.  
  14. #include <math.h>
  15.  
  16. #include "ansichek.h"
  17. #include "stdfn.h"
  18. #define GPFAR /**/
  19. #include "binary.h"
  20. #include "alloc.h"
  21.  
  22. void int_error __P((char *error_text, int dummy));
  23. float function __P((int p, double x, double y));
  24. int main __P((void));
  25.  
  26.  
  27. typedef struct {
  28.   float xmin, xmax;
  29.   float ymin, ymax;
  30. } range;
  31.  
  32. #define NUM_PLOTS 2
  33. range TheRange[] = {{-3,3,-2,2},
  34.                     {-3,3,-3,3},
  35.                     {-3,3,-3,3}};/* Sampling rate causes this to go from -3:6*/
  36.  
  37. /*---- Stubs to make this work without including huge libraries ----*/
  38. void int_error(error_text,dummy)
  39.      char *error_text;
  40.      int dummy;
  41. {
  42.   fprintf(stderr,"Fatal error..\n");
  43.   fprintf(stderr,"%s\n",error_text);
  44.   fprintf(stderr,"...now exiting to system ...\n");
  45.   exit(1);
  46. }
  47.  
  48. void FreeHelp(void)
  49. {
  50. }
  51.  
  52. /*---- End of stubs ----*/
  53.  
  54. float function(p,x,y)
  55. int p;
  56. double x,y;
  57. {
  58.   float t;
  59.   switch (p){
  60.   case 0:
  61.     t = 1.0/(x*x+y*y+1.0);
  62.     break;
  63.   case 1:
  64.     t = sin(x*x + y*y)/(x*x + y*y);
  65.     if (t> 1.0)  t = 1.0;
  66.     break;
  67.   case 2:
  68.     t = sin(x*x + y*y)/(x*x + y*y);
  69.     t *= sin(4.*(x*x + y*y))/(4.*(x*x + y*y));  /* sinc modulated sinc */
  70.     if (t> 1.0)  t = 1.0;
  71.     break;
  72.   default:
  73.     fprintf(stderr, "Unknown function\n");
  74.     break;
  75.   }
  76.   return t;
  77. }
  78.  
  79. #define ISOSAMPLES (double)5
  80. int
  81. main()
  82. {
  83.   int plot;
  84.   int i,j;
  85.   float x,y;
  86.   float *rt,*ct;
  87.   float **m;
  88.   int xsize,ysize;
  89.   char buf[256];
  90.   FILE *fout;
  91. /*  Create a few standard test interfaces */
  92.  
  93.   for(plot = 0; plot < NUM_PLOTS; plot++){
  94.     xsize = (TheRange[plot].xmax - TheRange[plot].xmin)*ISOSAMPLES +1;
  95.     ysize = (TheRange[plot].ymax - TheRange[plot].ymin)*ISOSAMPLES +1;
  96.  
  97.     rt = vector(0,xsize-1);
  98.     ct = vector(0,ysize-1);
  99.     m =  matrix(0,xsize-1,0,ysize-1);
  100.  
  101.     for(y=TheRange[plot].ymin,j=0;j<ysize;j++,y+=1.0/(double)ISOSAMPLES){
  102.       ct[j] = y;
  103.     }
  104.  
  105.     for(x=TheRange[plot].xmin,i=0;i<xsize;i++,x+=1.0/(double)ISOSAMPLES){
  106.       rt[i] = x;
  107.       for(y=TheRange[plot].ymin,j=0;j<ysize;j++,y+=1.0/(double)ISOSAMPLES){
  108.         m[i][j] = function(plot,x,y);
  109.       }
  110.     }
  111.  
  112.     sprintf(buf,"binary%d",plot+1);
  113. #ifdef UNIX
  114.     if(!(fout = fopen(buf,"w")))
  115. #else
  116.     if(!(fout = fopen(buf,"wb")))
  117. #endif
  118.       int_error("Could not open file",0);
  119.     else{
  120.       fwrite_matrix(fout,m,0,xsize-1,0,ysize-1,rt,ct);
  121.     }
  122.     free_vector(rt,0,ysize-1);
  123.     free_vector(ct,0,xsize-1);
  124.     free_matrix(m,0,xsize-1,0,ysize-1);
  125.   }
  126.  
  127.   /* Show that it's ok to vary sampling rate, as long as x1<x2, y1<y2... */
  128.   xsize = (TheRange[plot].xmax - TheRange[plot].xmin)*ISOSAMPLES +1;
  129.   ysize = (TheRange[plot].ymax - TheRange[plot].ymin)*ISOSAMPLES +1;
  130.  
  131.   rt = vector(0,xsize-1);
  132.   ct = vector(0,ysize-1);
  133.   m =  matrix(0,xsize-1,0,ysize-1);
  134.  
  135.   for(y=TheRange[plot].ymin,j=0;j<ysize;j++,y+=1.0/(double)ISOSAMPLES){
  136.     ct[j] = y>0?2*y:y;
  137.   }
  138.   for(x=TheRange[plot].xmin,i=0;i<xsize;i++,x+=1.0/(double)ISOSAMPLES){
  139.     rt[i] = x>0?2*x:x;
  140.     for(y=TheRange[plot].ymin,j=0;j<ysize;j++,y+=1.0/(double)ISOSAMPLES){
  141.       m[i][j] = function(plot,x,y);
  142.     }
  143.   }
  144.  
  145.   sprintf(buf,"binary%d",plot+1);
  146. #ifdef UNIX
  147.   if(!(fout = fopen(buf,"w")))
  148. #else
  149.   if(!(fout = fopen(buf,"wb")))
  150. #endif
  151.     int_error("Could not open file",0);
  152.   else{
  153.     fwrite_matrix(fout,m,0,xsize-1,0,ysize-1,rt,ct);
  154.   }
  155.   free_vector(rt,0,ysize-1);
  156.   free_vector(ct,0,xsize-1);
  157.   free_matrix(m,0,xsize-1,0,ysize-1);
  158.  
  159.   return(0);
  160. }
  161.